package com.example.bledemo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.bledemo.data.Devices;
import com.example.bledemo.R;

import java.util.List;

/**
 * 作者 : Leo
 * 邮箱 : wang100328@petalmail.com
 * 时间 : 2022/01/14
 * 描述 : 设备列表适配器
 */
public class DeviceListAdapter extends BaseAdapter {
    private final LayoutInflater layoutInflater;
    private final List<Devices> devices;

    public DeviceListAdapter(Context context, List<Devices> devices) {
        this.devices = devices;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return devices.size();
    }

    @Override
    public Object getItem(int position) {
        return devices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_device_listview, null);
            viewHolder = new ViewHolder();
            viewHolder.mTvAddress = convertView.findViewById(R.id.tv_device_listview_address);
            viewHolder.mTvName = convertView.findViewById(R.id.tv_device_listview_name);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.mTvName.setText(devices.get(position).getName());
        viewHolder.mTvAddress.setText(devices.get(position).getAddress());
        return convertView;
    }

    static class ViewHolder {
        TextView mTvName, mTvAddress;
    }
}
